package com.zany.children.action;

import com.zany.children.service.ChargesOwedDetailService;
import com.zany.children.service.ChargesOwedService;
import com.zany.children.vo.ChargesOwed;
import com.zany.children.vo.ChargesOwedDetail;
import com.zany.children.vo.PaymentProject;
import com.zany.system.vo.UserInfoVO;
import com.zany.util.ZanyErrorMsg;
import com.zany.util.ZanyException;
import com.zany.util.action.AbstractActionSupport;
import com.zany.util.ajax.AjaxTools;
import com.zany.util.json.JsonTools;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ReductionAction extends AbstractActionSupport {

    private Map session;

    private ChargesOwedDetailService chargesOwedDetailService;

    public ChargesOwedDetailService getChargesOwedDetailService() {
        return chargesOwedDetailService;
    }

    public void setChargesOwedDetailService(ChargesOwedDetailService chargesOwedDetailService) {
        this.chargesOwedDetailService = chargesOwedDetailService;
    }


    private ChargesOwedService chargesOwedService;

    public ChargesOwedService getChargesOwedService() {
        return chargesOwedService;
    }

    public void setChargesOwedService(ChargesOwedService chargesOwedService) {
        this.chargesOwedService = chargesOwedService;
    }

    private static Log logger = LogFactory.getLog(ReductionAction.class);

    @Override
    public void setSession(Map map) {
        this.session = map;
    }

    public void save() throws ZanyException {
        response = ServletActionContext.getResponse();
        String stuId = request.getParameter("stuId");
        String classId = request.getParameter("classId");
        String money = request.getParameter("money");
        String projectId = request.getParameter("projectId");
        ChargesOwedDetail chargesOwedDetail = new ChargesOwedDetail();
        chargesOwedDetail.setStuId(Long.parseLong(stuId));
        chargesOwedDetail.setClassId(Long.parseLong(classId));
        chargesOwedDetail.setMoney(Double.parseDouble(money));
        chargesOwedDetail.setProjectId(Long.parseLong(projectId));
        chargesOwedDetail.setPageMessage(1);
        List<ChargesOwedDetail> listPage = chargesOwedDetailService.getChargesOwedDetailListPage(chargesOwedDetail);
        if (listPage != null && listPage.size() > 0) {
            ChargesOwedDetail chargesOwedDetail1 = listPage.get(0);
            chargesOwedDetail1.setMoney(Double.parseDouble(money));
            chargesOwedDetailService.updateChargesOwedDetail(chargesOwedDetail1);
        }
        AjaxTools.exAjax("{success:true}", this.response);
    }


    public void list() throws ZanyException {
        String start = request.getParameter("start");
        String classId = request.getParameter("classId");
        UserInfoVO userInfo = (UserInfoVO) session.get("userInfo");
        response = ServletActionContext.getResponse();
        Integer pageStart = StringUtils.isBlank(start) ? 1 : Integer.parseInt(start);
        ChargesOwed chargesOwed = new ChargesOwed();
        chargesOwed.setPageMessage(pageStart);

        Map<String, Object> map = new HashMap<String, Object>();
        if (userInfo != null) {
            chargesOwed.setOrgCode(userInfo.getUserOrg());
        }
        if (StringUtils.isNotBlank(classId)) {
            chargesOwed.setClassId(Long.parseLong(classId));
        }
        Integer pageTotal = chargesOwedService.getChargesOwedListPageCount(chargesOwed);
        List<ChargesOwed> chargesOwedListPage = chargesOwedService.getChargesOwedListPage(chargesOwed);
        AjaxTools.exAjax("{results:" + pageTotal + ",ZANY:" + JsonTools.lstBeanToJson(chargesOwedListPage) + "}", response);
    }


    public void getProjectListByStuId() throws ZanyException {
        response = ServletActionContext.getResponse();
        String stuId = request.getParameter("stuId");
        String chargesType = request.getParameter("chargesType");
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("stuId", stuId);
        if (StringUtils.isBlank(chargesType)) {
            param.put("chargesType", 1);
        } else {
            param.put("chargesType", 2);
        }
        List<PaymentProject> list = chargesOwedDetailService.getProjectListByStuId(param);
        AjaxTools.exAjax("{results:" + true + ",ZANY:" + JsonTools.lstBeanToJson(list) + "}", response);
    }


    public void saveReduction() throws ZanyException {
        try {
            response = ServletActionContext.getResponse();
            String owedId = request.getParameter("id");
            String money = request.getParameter("money");
            String projectId = request.getParameter("reductioniProjectId");
            ChargesOwedDetail chargesOwedDetail = new ChargesOwedDetail();
            UserInfoVO userInfo = (UserInfoVO) session.get("userInfo");
            if (userInfo != null) {
                chargesOwedDetail.setCreater(userInfo.getUserName());
            }
            chargesOwedDetail.setMoney(Double.parseDouble(money));
            chargesOwedDetail.setOwedId(Long.parseLong(owedId));
            chargesOwedDetail.setProjectId(Long.parseLong(projectId));
            chargesOwedDetailService.updateReduction(chargesOwedDetail);
            AjaxTools.exAjax("{success:true}", this.response);
        } catch (Exception e) {
            AjaxTools.exAjax("{success:false}", response);
            throw new ZanyException(ZanyErrorMsg.ERR_DEFAULT, e.getMessage(), e);
        }
    }
}
